package com.balance.anole.client.retail.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.balance.anole.admin.to.ShopTO;
import com.balance.anole.business.to.BusinessDateTO;
import com.balance.anole.business.to.ShopReceiptTO;
import com.balance.anole.client.common.dao.hibernate.BaseDAOImpl;
import com.balance.anole.client.retail.dao.ShopRetailDAO;
import com.balance.anole.common.service.CommonService;
import com.balance.anole.utils.DateUtil;

public class ShopRetailDAOImpl extends BaseDAOImpl implements ShopRetailDAO {

	private CommonService commonService;

	public ShopReceiptTO updateReceiptToBeHeld(ShopReceiptTO receipt) {

		receipt.setProgress(commonService.getCodeLookTOByCodeTypeAndCodeId("PROGRESS", "H"));
		insertOrUpdateRetailRecord(receipt);
		List<ShopReceiptTO> tmpReceiptList = getReceiptByReceiptNum(receipt.getReceiptNum());
		if (!tmpReceiptList.equals(null) && tmpReceiptList.size()>0){
			return tmpReceiptList.get(0);
		}else{
			return null;
		}
	}

	public ShopReceiptTO updateReceiptToBeReleased(ShopReceiptTO receipt){
		receipt.setProgress(commonService.getCodeLookTOByCodeTypeAndCodeId("PROGRESS", "C"));
		insertOrUpdateRetailRecord(receipt);
		List<ShopReceiptTO> tmpReceiptList = getReceiptByReceiptNum(receipt.getReceiptNum());
		if (!tmpReceiptList.equals(null) && tmpReceiptList.size()>0){
			return tmpReceiptList.get(0);
		}else{
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<ShopReceiptTO> getListOfHeldReceiptForAgent(String agentId) {
		String hql = "from com.balance.anole.business.to.ShopReceiptTO as rcp where rcp.shop.shop.agentId = ? and rcp.progress.codeId = 'H' order by rcp.createDt desc";
		return getHibernateTemplate().find(hql,agentId);
	}

//	public void insertOrUpdateShopPayment(
//			List<ShopPaymentTO> paymentList) {
//		if (!paymentList.equals(null) && paymentList.size()>0){
//			for (int i=0; i<paymentList.size(); i++ ){
//				super.insertOrupdateBaseTO((ShopPaymentTO) paymentList.get(i));
//			}
//		}
//	}

//	public void insertOrUpdateShopReceipt(ShopReceiptTO receipt){
//		super.insertOrupdateBaseTO(receipt);
//	}

	@SuppressWarnings("unchecked")
	public List<ShopReceiptTO> getReceiptByReceiptNum(String receiptNum){
		return getHibernateTemplate().find("from com.balance.anole.business.to.ShopReceiptTO as rcp where rcp.receiptNum = ?",receiptNum);
	}

	@Override
	public void insertOrUpdateRetailRecord(ShopReceiptTO receipt) {
		if(receipt.getReceiptNum()==null || "".equals(receipt.getReceiptNum())){
			receipt.setReceiptNum(getNewReceiptNumOfShop(receipt.getShop(),receipt.getBusinessDate()));
		}

		super.insertOrupdateBaseTO(receipt);
	}

	private String generateReceiptNum(Date businessDate, String shopId, int sn){
		String numFormat = "%06d";
		return DateUtil.format(businessDate, DateUtil.DEFALT_FORMAT_INPUT_REPORT_DATE) + "-" + shopId + "-" + String.format(numFormat, sn);
	}

	@SuppressWarnings({"unchecked" })
	public String getNewReceiptNumOfShop(ShopTO shopTO, BusinessDateTO businessDate){
		String numFormat = "%06d";
		String firstReceiptNum = generateReceiptNum(businessDate.getBusinessDate(), shopTO.getShop().getAgentId(), 1);

		String hql = "select max(rcp.receiptNum) from com.balance.anole.business.to.ShopReceiptTO as rcp where rcp.shop = :shopTO and businessDate = :businessDate";
		Query q = getSession().createQuery(hql);
		q.setParameter("shopTO", shopTO);
		q.setParameter("businessDate", businessDate);

		List listOfMaxReceipt = q.list();

		if(listOfMaxReceipt == null || listOfMaxReceipt.size() == 0 || listOfMaxReceipt.get(0) == null){
			return firstReceiptNum;
		}else{
			String receiptNum = (String)listOfMaxReceipt.get(0);
			int max_Num = Integer.parseInt(receiptNum.substring(receiptNum.length()-6, receiptNum.length()));
			/*for(int i=0;i<listOfMaxReceipt.size();i++){
				ShopReceiptTO shopReceiptTO = (ShopReceiptTO)listOfMaxReceipt.get(i);
				String receiptNum = shopReceiptTO.getReceiptNum();
				if(receiptNum == null || receiptNum.length()<6){
					continue;
				}else{
					int receipt_Num = Integer.parseInt(receiptNum.substring(receiptNum.length()-6, receiptNum.length()));
					if(receipt_Num > max_Num){
						max_Num = receipt_Num;
					}
				}
			}*/

			max_Num++;

			/*int needzeroNum = 6-((max_Num+"").length());
			String max_NumString = max_Num + "";
			for(int i=0;i<needzeroNum; i++){
				max_NumString = "0" + max_NumString;
			}*/

			return generateReceiptNum(businessDate.getBusinessDate(), shopTO.getShop().getAgentId(), max_Num);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ShopReceiptTO> getShopReceipts4Upload(ShopTO shopTO, BusinessDateTO bizDt,
			Integer batchSize) {
		String hql = "from com.balance.anole.business.to.ShopReceiptTO as rcp where rcp.shop = :shopTO and businessDate = :businessDate";
		Query q = getSession().createQuery(hql);
		q.setParameter("shopTO", shopTO);
		q.setParameter("businessDate", bizDt);
		if( batchSize != null && batchSize.intValue() > 0){
			q.setFetchSize(batchSize);
		}
		return q.list();
	}

	@Override
	public void deleteReceipt(ShopReceiptTO receipt) {
		getHibernateTemplate().delete(receipt);
	}
}
